package com.aracky.system.controller;

import com.aracky.common.util.ConvertUtil;
import com.aracky.common.vo.CommonResult;
import com.aracky.common.vo.PageVO;
import com.aracky.system.model.entity.SysRole;
import com.aracky.system.model.entity.SysRoleMenu;
import com.aracky.system.model.entity.SysUser;
import com.aracky.system.model.entity.SysUserRole;
import com.aracky.system.model.form.SysRoleForm;
import com.aracky.system.model.form.SysRoleMenuForm;
import com.aracky.system.model.form.SysRoleUserForm;
import com.aracky.system.model.request.SysRoleRequest;
import com.aracky.system.model.request.SysRoleUserRequest;
import com.aracky.system.model.vo.SysDictTypeVO;
import com.aracky.system.model.vo.SysRoleVO;
import com.aracky.system.model.vo.SysUserVO;
import com.aracky.system.service.SysRoleMenuService;
import com.aracky.system.service.SysRoleService;
import com.aracky.system.service.SysUserRoleService;
import com.aracky.system.service.SysUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色信息表 前端控制器
 * </p>
 *
 * @author zzp
 * @since 2022-07-14 11:22:57
 */
@RestController
@RequestMapping("/sysRole")
@Api(tags = "角色相关接口")
public class SysRoleController {
    @Resource
    private SysRoleService sysRoleService;

    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    /**
     * 角色新增
     *
     * @param sysRoleForm
     * @return
     */
    @ApiOperation(value = "新增")
    @PostMapping("/save")
    public CommonResult save(@RequestBody SysRoleForm sysRoleForm) {
        if (sysRoleService.getCheckRoleName(sysRoleForm.getRoleName()) > 0) {
            return CommonResult.error("角色名已存在!");
        }
        sysRoleService.saveRole(ConvertUtil.sourceToTarget(sysRoleForm, SysRole.class));
        return CommonResult.success("操作成功");
    }

    /**
     * 删除角色
     *
     * @param roleIds
     * @return
     */
    @ApiOperation(value = "删除")
    @DeleteMapping("/{roleIds}")
    public CommonResult delete(@PathVariable Long[] roleIds) {
        List<Long> list=new ArrayList<>();
        for (Long roleId:roleIds){
            list.add(roleId);
        }
        sysRoleService.removeBatchByIds(list);
        return CommonResult.success("操作成功");
    }

    /**
     * 修改角色
     *
     * @param sysRoleForm
     * @return
     */
    @ApiOperation(value = "修改")
    @PutMapping("/update")
    public CommonResult update(@RequestBody SysRoleForm sysRoleForm) {
        sysRoleService.updateRole(ConvertUtil.sourceToTarget(sysRoleForm, SysRole.class));
        return CommonResult.success("操作成功");
    }

    /**
     * 查询详情
     *
     * @param roleId
     * @return
     */
    @ApiOperation(value = "查询详情")
    @GetMapping("/{roleId}")
    public CommonResult get(@PathVariable Long roleId) {
        SysRole sysRole = sysRoleService.getRoleById(roleId);
        SysRoleVO sysRoleVO = ConvertUtil.sourceToTarget(sysRole, SysRoleVO.class);
        return CommonResult.success(sysRoleVO, "操作成功");
    }

    /**
     * 查询角色列表
     *
     * @param sysRoleRequest
     * @return
     */
    @ApiOperation(value = "角色分页查询")
    @PostMapping(value = "/pageList")
    public CommonResult<PageVO<SysRoleVO>> pageList(@RequestBody SysRoleRequest sysRoleRequest) {
        PageVO<SysRoleVO> pageVO = sysRoleService.getRolePage(sysRoleRequest);
        return CommonResult.success(pageVO);
    }

    /**
     * 分配用户给角色
     *
     * @param sysRoleUserForm
     * @return
     */
    @ApiOperation(value = "分配用户给角色")
    @PostMapping("/bind/user")
    public CommonResult saveUserToRole(@RequestBody SysRoleUserForm sysRoleUserForm) {
        sysRoleService.saveUserToRole(sysRoleUserForm.getRoleId(), sysRoleUserForm.getUserIds());
        return CommonResult.success("操作成功");
    }

    /**
     * 分配菜单给角色
     *
     * @param sysRoleMenuForm
     * @return
     */
    @ApiOperation(value = "分配菜单给角色")
    @PostMapping("/bind/menu")
    public CommonResult saveMenuToRole(@RequestBody SysRoleMenuForm sysRoleMenuForm) {
        sysRoleService.saveMenuToRole(sysRoleMenuForm.getRoleId(), sysRoleMenuForm.getMenuIds());
        return CommonResult.success("操作成功");
    }


    @ApiOperation(value = "通过角色id查询绑定的用户")
    @PostMapping("/getUser/withRoleId")
    public CommonResult<PageVO<List<SysUserVO>>> getUser(@RequestBody SysRoleUserRequest roleUserRequest) {
        LambdaQueryWrapper<SysUserRole> userRoleLambdaQueryWrapper
                = Wrappers.<SysUserRole>lambdaQuery().eq(SysUserRole::getRoleId, roleUserRequest.getRoleId());
        List<SysUserRole> userRoleList = sysUserRoleService.list(userRoleLambdaQueryWrapper);
        PageVO pageVO = PageVO.convertPage(roleUserRequest.toPage());
        if(userRoleList == null || userRoleList.isEmpty()){
            pageVO.setData(new ArrayList());
            return CommonResult.success(pageVO,"查询成功");
        }

        List<Long> userIdList = userRoleList.stream().map(SysUserRole::getUserId).collect(Collectors.toList());

        LambdaQueryWrapper<SysUser> lambdaQueryWrapper = Wrappers.<SysUser>lambdaQuery()
                .select(SysUser::getUserId, SysUser::getUserName)
                .in(SysUser::getUserId, userIdList);
        Page<SysUser> page = sysUserService.page(roleUserRequest.toPage(),lambdaQueryWrapper);

         pageVO = PageVO.convertPage(page);
        List<SysUserVO> sysUserVOList = ConvertUtil.sourceToTargets(page.getRecords(), SysUserVO.class);
        pageVO.setData(sysUserVOList);

        return CommonResult.success(pageVO, "查询成功");
    }

    @ApiOperation(value = "通过角色id查询绑定的菜单")
    @GetMapping("/getMenu/withRoleId")
    public CommonResult<List<Long>> getMenu(Long roleId) {
        LambdaQueryWrapper<SysRoleMenu> roleMenuLambdaQueryWrapper = Wrappers.<SysRoleMenu>lambdaQuery()
                .select(SysRoleMenu::getMenuId)
                .eq(SysRoleMenu::getRoleId, roleId);
        List<SysRoleMenu> roleMenuList = sysRoleMenuService.list(roleMenuLambdaQueryWrapper);

        List<Long> menuIdList = null;
        if (roleMenuList == null || roleMenuList.isEmpty()) {
            return CommonResult.success(menuIdList, "查询成功");
        }

        menuIdList = roleMenuList.stream().map(SysRoleMenu::getMenuId).collect(Collectors.toList());
        return CommonResult.success(menuIdList, "查询成功");
    }


}
